package com.qys.livesMall.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qys.livesMall.system.mapper.SysShareConfigMapper;
import com.qys.livesMall.system.model.dto.sys.H2SysShareConfigDTO;
import com.qys.livesMall.system.model.entity.H2SysShareConfig;
import com.qys.livesMall.system.model.vo.sys.H2SysShareConfigVO;
import com.qys.livesMall.system.service.SysShareConfigService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dp
 * Date: 2023/06/06
 * Time: 11:19
 * DESCRIPTION:
 */
@Service
public class SysShareConfigServiceImpl extends ServiceImpl<SysShareConfigMapper, H2SysShareConfig> implements SysShareConfigService {

    /**
     * 查询层级分成比例设置
     *
     * @param type 0店铺,1用户
     */
    @Override
    public List<H2SysShareConfigVO> list(Byte type) {
        List<H2SysShareConfig> shareConfigs = baseMapper.selectList(Wrappers.<H2SysShareConfig>query().eq("type", type)
                .orderByAsc("hierarchy"));
        if (CollectionUtils.isNotEmpty(shareConfigs)) {
            return BeanUtil.copyToList(shareConfigs, H2SysShareConfigVO.class);
        }

        return new ArrayList<>();
    }

    /**
     * 新增层级配置信息
     *
     * @param config {@link  H2SysShareConfigDTO}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(H2SysShareConfigDTO config) {
        H2SysShareConfig sysShareConfig = new H2SysShareConfig();
        BeanUtil.copyProperties(config, sysShareConfig);
        saveOrUpdate(sysShareConfig);
    }
}
